import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AssessPriceService} from '../../assess-price.service';
import {NzMessageService} from 'ng-zorro-antd/message';

interface Query {
  templateName: string;
  templateRemark: string;
  date: any;
  page: number;
  pageSize: number;
}

@Component({
  selector: 'app-assess-price-template-list',
  templateUrl: './assess-price-template-list.component.html',
  styleUrls: ['./assess-price-template-list.component.scss']
})
export class AssessPriceTemplateListComponent implements OnInit {
  // 查询条件
  query: Query = {
    templateName: '',
    templateRemark: '',
    date: [],
    // 当前页
    page: 1,
    // 分页大小
    pageSize: 20,
  };
  listOfData = {
    page: 1,
    total: 100,
    pageSize: 20,
    records: []
  };

// 预览模态框显隐
  previewAssessPriceVisible = false;
  // 预览数据
  questionList: any;
  refreshLoading = false;

  constructor(
    private router: Router,
    private assessPriceService: AssessPriceService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.getTableData();
  }

  queryBtn() {
    this.query.page = 1;
    this.getTableData();
  }

  getTableData(
    title = this.query.templateName,
    remark = this.query.templateRemark,
    startTime = this.assessPriceService.formatTime(this.query.date[0], 'date'),
    endTime = this.assessPriceService.formatTime(this.query.date[1], 'date')) {
    this.assessPriceService.getTemplateList(title, remark, startTime, endTime, this.query.page, this.query.pageSize).then((res: any) => {
      console.log(res);
      if (res.code === 0) {
        this.listOfData = res.data;
      } else {
        this.nzMessageService.error(res.message);
      }
    }).catch(() => {
      this.nzMessageService.error('查询列表异常');
    });
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    this.getTableData();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.getTableData();
  }

  resetCondition() {
    this.query = {
      templateName: '',
      templateRemark: '',
      date: [],
      // 当前页
      page: 1,
      // 分页大小
      pageSize: 20,
    };
    this.getTableData();
  }

  // 去新增
  gotoPage() {
    this.router.navigate(['aisi-recyc/assessPriceTemplateEdit'], {queryParams: {operationType: 'add'}});
  }

  // 去修改
  modify(templateInfo: any) {
    this.router.navigate(['aisi-recyc/assessPriceTemplateEdit'], {
      queryParams: {
        operationType: 'modify',
        templateInfo: JSON.stringify(templateInfo)
      }
    });
  }

  // 去复制
  copy(templateInfo: any) {
    this.router.navigate(['aisi-recyc/assessPriceTemplateEdit'], {
      queryParams: {
        operationType: 'copy',
        templateInfo: JSON.stringify(templateInfo)
      }
    });
  }

  // 去查看
  viewTemplate(templateInfo: any) {
    this.router.navigate(['aisi-recyc/assessPriceTemplateEdit'], {
      queryParams: {
        operationType: 'view',
        templateInfo: JSON.stringify(templateInfo)
      }
    });
  }

  // 删除行
  deleteRow(id: any) {
    this.assessPriceService.deleteTemplate(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.getTableData();
      } else {
        this.nzMessageService.error(res.message);
      }
    }).catch(() => {
      this.nzMessageService.error('删除异常');
    });

  }

  // 预览
  preview(context: any) {
    this.questionList = JSON.parse(context as string);
    this.previewAssessPriceVisible = true;
  }

  toAssessType(data: any) {
    if (data.modelNum > 0) {
      this.router.navigate(['aisi-recyc/assessTypeManageList'], {
        queryParams: {
          templateId: data.id,
        }
      });
    }
  }

  refresh() {
    this.refreshLoading = true;
    this.assessPriceService.refreshTemplate().then(() => {
      this.nzMessageService.success('刷新成功');
      this.refreshLoading = false;
      this.getTableData();
    });
  }
}
